<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>注册</title>
    <!-- 引入css文件 -->
    <link rel="stylesheet" href="../../css/LXW/register.css" type="text/css"/>
    <link rel="stylesheet"  href="../../js/LXW/shipei.js">
</head>
<body>
    <div class="top-bar">
        <!-- <div class="login-text"><div>注册页面</div></div> -->

        <div class="input">
        <div class="username">
            <span>用户名:</span>
            <input id="username" type="text" placeholder="请输入用户名">
        </div>
        <div class="password">
            <span>密&nbsp;&nbsp;&nbsp;码:</span>
            <input id="password" type="password" placeholder="请输入密码">
        </div>
        <div class="email">
            <span>邮&nbsp;&nbsp;&nbsp;箱:</span>
            <input id="email" type="email" placeholder="请输入邮箱">
        </div>
        </div> 
        
        <div class=login>
            <button><a href="./login.html">返回登录</a></button>
            <button class="submit">注册</button>
        </div>
    </div>
</body>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <script>
        let usernameEl = document.querySelector('#username');
        let passwordEl = document.querySelector('#password');
        let emailEl = document.querySelector('#email');
        let submitBtn = document.querySelector('.submit');


        submitBtn.onclick = async function () {
            let username = usernameEl.value;
            let password = passwordEl.value;
            let email = emailEl.value;
            console.log(username);
            let result = await axios({
                method: 'post',
                url: 'http://127.0.0.1:9527/api/users/register',
                data: {
                    username,
                    password,
                    email,
                },
            })

            let data = result.data;
            if (data.code !== 0) {
                return alert(data.msg)
            } else {
                alert('注册成功,点击确定跳转')
                let timer = setTimeout(() =>{
                    location.href='./login.html'
                })
            }
            
        }
    </script>
</html>